"""团购活动页面"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from page.page_font_login import LoginPage
from selenium import webdriver
from time import sleep
class GroupBuy():
    def __init__(self, driver):
        self.driver = driver
    def cuxi(self):
    # 操作方法
    # 点击促销
        self.driver.find_element(By.CSS_SELECTOR, "#menu-ul > li.collapse.lis.ico_2").click()

    def clickgroup(self):
        # 点击添加团购
        self.driver.find_element(By.XPATH, "/html/body/div[2]/div[1]/ul/li[2]/ul/li[5]/a").click()
        #退出菜单的frame
        self.driver.switch_to.parent_frame()
        # 切入主界面的frame
        self.driver.switch_to.frame("main-frame")
    def addgroup(self):
        #添加活动
        self.driver.find_element(By.XPATH,"/html/body/h1/span[1]/a").click()
    def search(self):
        #点击搜索
        self.driver.find_element(By.XPATH,'/html/body/div[1]/form/input[2]').click()
    def selecta(self):
        #选择商品
        self.driver.find_element(By.CSS_SELECTOR, "#group-table > tbody > tr:nth-child(1) > td:nth-child(2) > select").send_keys("现货发售 马海毛保暖毛衣")

    def input_price(self):
        self.driver.find_element(By.CSS_SELECTOR,"#group-table > tbody > tr:nth-child(7) > td:nth-child(2) > input[type=text]:nth-child(1)").send_keys("1")
        sleep(4)
        self.driver.find_element(By.CSS_SELECTOR,'#group-table > tbody > tr:nth-child(7) > td:nth-child(2) > input[type=text]:nth-child(2)').send_keys("50")
        sleep(4)
    def click_btn(self):
        #点击确定
        self.driver.find_element(By.XPATH,'/html/body/form/div/table/tbody/tr[9]/td[2]/input[2]').click()
        sleep(3)

        #验证活动是否添加成功
    def get_page(self):
        self.driver.get("http://192.168.4.54:8081/upload/")
    #输入搜索内容
    def input_keys(self):
        self.driver.find_element(By.XPATH, "//*[@id='keyword']").send_keys("现货发售 马海毛保暖毛衣")

    #点击搜索
    def search_btn(self):
        self.driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/form/span[2]").click()

    #点击搜索的商品
    def photo(self):
        self.driver.find_element(By.XPATH, "//*[@id='compareForm']/div/div/a/img").click()
    ##点击立即购买
    def ecs_formbuy(self):
        self.driver.find_element(By.XPATH,"//*[@id='ECS_FORMBUY']/ul[3]/li[2]/a/img").click()
    #点击购物中心
    def center_buy(self):
        self.driver.find_element(By.CSS_SELECTOR,"body > div.block.table > div.flowBox > table > tbody > tr > td:nth-child(2) > a > img").click()
    sleep(4)
    #登录
    def lg_user(self):
        self.driver.find_element(By.XPATH,"//*[@id='username']").send_keys("lwx")
        self.driver.find_element(By.XPATH, "//*[@id='loginForm']/table/tbody/tr[2]/td[2]/input").send_keys("123456")
        self.driver.find_element(By.XPATH,"//*[@id='loginForm']/table/tbody/tr[5]/td/div/input[1]").click()
    def use_redbao(self):
        self.driver.find_element(By.CSS_SELECTOR,"#ECS_BONUS").send_keys("红包[￥222.00元]")

    def submit_buy(self):
        self.driver.find_element(By.CSS_SELECTOR,"#theForm > div:nth-child(14) > div:nth-child(3) > input[type=image]:nth-child(1)").click()
        sleep(5)


    #汇总
    def addgroupbuy(self):
        self.cuxi()
        self.clickgroup()
        self.addgroup()
        self.search()
        self.selecta()
        self.click_btn()
        self.input_price()
        self.get_page()
        self.input_keys()
        self.search_btn()
        self.photo()
        sleep(3)
        self.ecs_formbuy()
        sleep(3)
        self.center_buy()
        sleep(3)
        self.lg_user()
        sleep(5)
        self.use_redbao()
        sleep(5)
        self.submit_buy()
        sleep(3)